var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ifShowPasswordTips: false,
    buthidden: false,
    moneyNum: '',
    amount: '',
    bankCardName: '',
    bankCardNumber: '',
    bankCard: '',
    payStatus: '',
    ifTrue: false,
    Length: 6, //输入框个数
    isFocusOne: true, //聚焦
    ValueOne: '', //输入的内容
    ispassword: true //是否密文显示 true为密文， false为明文。
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.setData({
      amount: options.amount
    })
  },

  //提现金额验证
  onNumberMoney: function(e) {
    var that = this
    var price = e.detail.value
    if (!/^(([1-9]\d*[.]\d\d?)|([0][.]\d\d?)|([1-9]\d*)|0)$/.test(price)) {
      price = /[1-9]\d*[.]\d\d?/.exec(e.detail.value)
      price = price ? price : /[0][.]\d\d?/.exec(e.detail.value)
      price = price ? price : /[1-9]\d*/.exec(e.detail.value)
      price = price == '' || price == null ? '' : price
      price = /^[^.]+[.]$/.test(e.detail.value) ? price + '.' : price
    }
    if (price != '') {
      that.setData({
        moneyNum: price
      })
    }
  },
  onSubmit: function() {
    var that = this
    var amount = Number(that.data.amount).toFixed(2)
    var moneyNum = Number(that.data.moneyNum).toFixed(2)
    var payStatus = that.data.payStatus

    if (payStatus == 1) {
      if (!moneyNum) {
        wx.showToast({
          title: '请输入金额',
          icon: 'none',
          duration: 1500
        })
        return false
      } else if (parseFloat(moneyNum) > parseFloat(amount)) {
        wx.showToast({
          title: '余额不足',
          icon: 'none',
          duration: 1500
        })
        return false
      } else if (moneyNum < 10) {
        wx.showModal({
          title: '最低提现金额为10元',
          confirmColor: '#56BDE0',
          showCancel: false
        })
        return false
      } else if (that.data.ValueOne == '') {
        // wx.showToast({
        //   title: '支付密码必填',
        //   icon: "none",
        //   duration: 1500
        // })
        that.setData({ ifShowPasswordTips: true })
        return false
      } else if (that.data.ValueOne.length != 6) {
        wx.showToast({
          title: '密码长度有误',
          icon: 'none',
          duration: 1500
        })
        return false
      } else {
        // that.setData({ ifShowPasswordTips: true });
        that.setData({ buthidden: true })
        wx.request({
          method: 'POST',
          url: app.preUrl + 'toPutForward',
          dataType: 'json',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            userId: app.globalData.userId,
            amount: moneyNum,
            password: that.data.ValueOne //// 有问题
          },
          success: function(res) {
            if (res.data.code == 100) {
              console.log(res.data)
              wx.showToast({
                title: '提现申请成功',
                icon: 'success',
                duration: 1000
              })
              setTimeout(function() {
                wx.redirectTo({
                  url:
                    'cashProgress?bankCardName=' +
                    that.data.bankCardName +
                    '&bankCardNumber=' +
                    that.data.bankCardNumber +
                    '&price=' +
                    that.data.moneyNum
                })
              }, 1500)
            } else {
              wx.showModal({
                title: '提示',
                content: res.data.message,
                cancelText: '忘记密码',
                confirmText: '重试',
                cancelColor: '#50BEC5',
                confirmColor: '#666',
                success(res) {
                  if (res.confirm) {
                    that.setData({
                      ifShowPasswordTips: true,
                      ValueOne: ''
                    }) //重试
                  } else if (res.cancel) {
                    that.goCheckSetPaymentPassword() //忘记密码
                  }
                }
              })
              that.setData({ buthidden: false })
            }
          },
          fail: function(error) {
            console.log(error)
            that.setData({ buthidden: false })
          }
        })
      }
    } else if (payStatus == 2) {
      wx.showModal({
        title: '提示',
        content: '为了保障您的资金安全，请设置支付密码用于后续余额提现或使用',
        cancelText: '取消',
        confirmText: '去设置',
        cancelColor: '#666',
        confirmColor: '#50BEC5',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url:
                '../mine/checkSetPaymentPassword?phone=' +
                that.data.phone +
                '&type=2' //设置支付密码
            })
          } else if (res.cancel) {
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    this.dataInit()
  },
  onMineInfo: function() {
    var that = this
    return new Promise(function(resolve, reject) {
      wx.request({
        method: 'POST',
        url: app.preUrl + 'findByUserId',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: app.globalData.userId
        },
        success: function(res) {
          if (res.data.code == 100) {
            let data = res.data.result
            console.log(data)
            resolve(data)
          } else {
            reject('服务器状态异常')
          }
        },
        fail: function(error) {
          console.log(error)
        }
      })
    })
  },
  getBlankInfo() {
    var that = this
    return new Promise(function(resolve, reject) {
      wx.request({
        method: 'POST',
        url: app.preUrl + 'bankUserId',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: app.globalData.userId
        },
        success: function(res) {
          console.log(res)
          if (res.data.code == 100) {
            console.log(res.data.result)
            let data = res.data.result
            resolve(data)
          } else {
            reject('服务器状态异常')
          }
        },
        fail: function(error) {
          console.log(error)
        }
      })
    })
  },
  dataInit() {
    let that = this
    wx.showLoading({ title: '加载中' })
    Promise.all([this.getBlankInfo(), this.onMineInfo()])
      .then(res => {
        let [BlankInfo, MineInfo] = res
        wx.hideLoading()
        that.setData({
          bankCardName: BlankInfo.bankCardName,
          bankCardNumber: BlankInfo.bankCardNumber,
          bankCard: BlankInfo.bankCardNumber.substr(-4),
          payStatus: MineInfo.payStatus,
          wxType: MineInfo.wxType,
          phone: MineInfo.telephone
        })
      })
      .catch(res => {
        //console.log(res)
        //服务器状态异常提示
        wx.showToast({ title: '服务器异常', icon: 'none', duration: 1000 })
      })
  },
  onFocusOne: function(e) {
    var that = this
    var inputValue = e.detail.value.trim()
    that.setData({
      ValueOne: inputValue
    })
    if (e.detail.value.length >= 6) {
      that.setData({ ifShowPasswordTips: false })
      that.onSubmit()
    }
  },

  onTapOne: function(e) {
    console.log(e)
    console.log(e.currentTarget.dataset.index)
    var that = this
    that.setData({
      isFocusOne: true
    })
  },

  onRightCancel: function() {
    var that = this
    that.setData({ ValueOne: '', ifShowPasswordTips: false })
  },

  //忘记密码
  goCheckSetPaymentPassword: function() {
    var that = this
    var wxType = that.data.wxType
    var phone = that.data.phone
    wx.navigateTo({
      url: '../mine/checkSetPaymentPassword?phone=' + phone + '&type=2' //忘记密码
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
})
